<template>
  <el-dialog
    title="分配角色"
    :visible="showDialog"
    @close="closeFn"
  >
    <el-checkbox-group v-model="checkList">
      <el-checkbox
        v-for="item in allList"
        :key="item.id"
        :label="item.id"
      >{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button size="small" @click="closeFn">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>
<script>
import { roleList } from '@/api/setting'
import { assignRole, employeesInfo } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      checkList: [], // 选中的选项
      allList: [], // 所有的选项
      userId: '' // 员工id
    }
  },
  async created() {
    const res = await roleList({
      page: 1,
      pagesize: 1000
    })
    this.allList = res.data.data.rows
  },
  methods: {
    // 确定按钮
    async submit() {
      await assignRole({
        id: this.userId, // 员工id
        roleIds: this.checkList
      })
      // 关闭弹窗
      this.closeFn()
    },
    async getEmployeeDetail(id) {
      this.userId = id // 保存员工id
      const res = await employeesInfo(id)
      // 回显
      this.checkList = res.data && res.data.data.roleIds || []
    },
    // 关闭弹窗按钮
    closeFn() {
      this.$emit('update:showDialog', false)
      // 清空选项
      this.checkList = []
    }
  }
}
</script>
<style>

</style>
